/*
 * CurrentLocationService.cpp
 *
 *  Created on: Jun 15, 2011
 *      Author: ToanNhan
 */

#include "../inc/Model/CurrentLocationService.h"
#include "../inc/Ui/CameraPreviewForm.h"
CurrentLocationService* CurrentLocationService::pInstance = 0;

CurrentLocationService::CurrentLocationService(){
	pLocProvider = new LocationProvider();
	members = new ArrayListT<ICurrentLocationMember*>();
	oldLatitude = oldLongitude = Float::GetMaxValue();

}

void CurrentLocationService::GetCurrentLocation(float &longitude, float &latitude){
	if (oldLatitude == Float::GetMaxValue()){
		latitude = 10.762827;
		longitude = 106.682551;
	}else{
		longitude = oldLongitude;
		latitude = oldLatitude;
	}

}

void CurrentLocationService::Construct(){
	result r = E_SUCCESS;
	r = pLocProvider->Construct(LOC_METHOD_HYBRID);
	if (IsFailed(r)){
		return;
	}
	pLocProvider->RequestLocationUpdates(*this, 10, false);

	r = members->Construct();
	if (IsFailed(r)){
		return;
	}
//	oldLongitude = oldLatitude = Float::GetMaxValue();

}
CurrentLocationService::~CurrentLocationService(){

	if (members != null){
		members->RemoveAll();
		delete members;
		members = null;
	}

	if (pLocProvider != null){
		delete pLocProvider;
		pLocProvider = null;
	}
}

void CurrentLocationService::AddMember(ICurrentLocationMember &member){
	this->members->Add(&member);
}

void CurrentLocationService::RemoveMember(ICurrentLocationMember &member){
	members->Remove(&member);
}

CurrentLocationService* CurrentLocationService::getInstance(){
	if (pInstance == null){
		pInstance = new CurrentLocationService();
		pInstance->Construct();
	}
	return pInstance;
}


float CurrentLocationService::getLatitude() {
	return oldLatitude;
}
float CurrentLocationService::getLongitude() {
	return oldLongitude;
}
void CurrentLocationService::OnLocationUpdated(Location &location){
	const QualifiedCoordinates* pQCoord = location.GetQualifiedCoordinates();
	if (pQCoord != null) {
		float latitude = pQCoord->GetLatitude();
		float longitude = pQCoord->GetLongitude();
		if (longitude != oldLongitude || latitude != oldLatitude){
			oldLongitude = longitude;
			oldLatitude = latitude;
			int memberCount = members->GetCount();
			ICurrentLocationMember *member;
			for (int i = 0; i < memberCount; i++){
				members->GetAt(i, member);
				if (member != null){
					member->LocationUpdate(longitude, latitude);
				}
			}
			/*Frame* pFrame =
								Application::GetInstance()->GetAppFrame()->GetFrame();
						CameraForm* pCameraForm =
								static_cast<CameraForm*> (pFrame->GetControl(L"CameraPreview"));
								if(pCameraForm==null)
								{
									AppLog("Khong ton tai camera");
								}
								else
								{
									if(pCameraForm->IsStarted()==true)//Van chua thoat ra khoi che do camera
									{
										AppLog("Ton Tai Camera");
										pCameraForm->YeuCauCapNhatThongTinDiaDiemVaThoiTiet(latitude, longitude);
												}
										}
										*/
		}
	}
}
